package com.molichuxing.services.sdk.wxpay;


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

import com.molichuxing.framework.utils.PropertiesUtil;


public class WXPayConfigImpl extends WXPayConfig {

	/**
	 * 是否微信沙箱环境（true:是）
	 */
	private Boolean useSandbox;
	/**
	 * 公众账号ID
	 */
	private String appId;
	/**
	 * 商户号
	 */
	private String mchId;

	/**
	 * 密钥
	 */
	private String key;
	/**
	 * 
	 */
	private String secret;
	/**
	 * 支付成功返回地址
	 */
	private String payNotify;

	/**
	 * 证书
	 */
	private byte[] certData;

	/**
	 * 公众号实例
	 */
	private static WXPayConfigImpl jsapiInstance;

	/**
	 * app实例
	 */
	private static WXPayConfigImpl appInstance;

	/**
	 * 小程序实例
	 */
	private static WXPayConfigImpl appletInstance;

	public static WXPayConfigImpl getJsapiInstance() throws Exception {
		if (jsapiInstance == null) {
			synchronized (WXPayConfigImpl.class) {
				if (jsapiInstance == null) {
					Map<String, String> properties = PropertiesUtil.getInstance()
							.getAllProperties("config.properties");
					jsapiInstance = new WXPayConfigImpl(properties.get("wechat.useSandbox"),
							properties.get("wechat.jsapi.appid"), properties.get("wechat.jsapi.mchid"),
							properties.get("wechat.jsapi.key"), properties.get("wechat.secret"),
							properties.get("wechat.pay.notifyUrl"));
				}
			}
		}
		return jsapiInstance;
	}

	public static WXPayConfigImpl getAppInstance() throws Exception {
		if (appInstance == null) {
			synchronized (WXPayConfigImpl.class) {
				if (appInstance == null) {
					Map<String, String> properties = PropertiesUtil.getInstance().getAllProperties("config.properties");
					appInstance = new WXPayConfigImpl(properties.get("wechat.useSandbox"),
							properties.get("wechat.app.appid"), properties.get("wechat.app.mchid"),
							properties.get("wechat.app.key"), properties.get("wechat.secret"),
							properties.get("wechat.pay.notifyUrl"));
				}
			}
		}
		return appInstance;
	}

	public static WXPayConfigImpl getAppletInstance() throws Exception {
		if (appletInstance == null) {
			synchronized (WXPayConfigImpl.class) {
				if (appletInstance == null) {
					Map<String, String> properties = PropertiesUtil.getInstance().getAllProperties("config.properties");
					appletInstance = new WXPayConfigImpl(properties.get("wechat.useSandbox"),
							properties.get("wechat.applet.appid"), properties.get("wechat.applet.mchid"),
							properties.get("wechat.applet.key"), properties.get("wechat.applet.secret"),
							properties.get("wechat.pay.notifyUrl"));
				}
			}
		}
		return appletInstance;
	}

	private WXPayConfigImpl(String useSandbox, String appId, String mchId, String key, String secret,
			String payNotify) {
		if ("true".equals(useSandbox)) {
			this.useSandbox = true;
		} else {
			this.useSandbox = false;
		}
		this.appId = appId;
		this.mchId = mchId;
		this.key = key;
		this.secret = secret;
		this.payNotify = payNotify;
	}

	public String getPayNotify() {
		return this.payNotify;
	}

	public Boolean getUseSandbox() {
		return useSandbox;
	}

	public String getSecret() {
		return this.secret;
	}

	@Override
	public String getAppID() {
		return this.appId;
	}

	@Override
	public String getMchID() {
		return this.mchId;
	}

	@Override
	public String getKey() {
		return this.key;
	}

	@Override
	public InputStream getCertStream() {
		ByteArrayInputStream certBis;
		certBis = new ByteArrayInputStream(this.certData);
		return certBis;
	}

	@Override
	public int getHttpConnectTimeoutMs() {
		return 2000;
	}

	@Override
	public int getHttpReadTimeoutMs() {
		return 10000;
	}

	@Override
    IWXPayDomain getWXPayDomain() {
		return WXPayDomainSimpleImpl.instance();
	}

	public String getPrimaryDomain() {
		return "api.mch.weixin.qq.com";
	}

	public String getAlternateDomain() {
		return "api2.mch.weixin.qq.com";
	}

}
